var vaccineArr=[];
var vaccineIdArr=[];
$(function () {
    getAll();
    getbaseList2();
    var ageArr=["出生","1月龄","2月龄","3月龄","4月龄","5月龄","6月龄","8月龄","7月龄","9月龄","11月龄","1周岁","1岁半","2周岁","3周岁","4周岁","6周岁","9周岁"];
    var ageHtml="";
    for(var i=0;i<ageArr.length;i++){
        ageHtml+="<option value='"+ageArr[i]+"'>"+ageArr[i]+"</option>"
    }
    $("#ageSec").append(ageHtml);
});
/*重置*/
function resetClick(){
    $("#vaccName").find("option[value = '']").prop("selected","selected");
    getbaseList();
    getbaseList2();
}
/*查询所有疫苗*/
function getAll(){
    vaccineArr=[];
    vaccineIdArr=[];
    $.ajax({
        url : jypath + "/vaccine/queryVaccineType",
        type : "post",
        data:{
            "pageNum":1,
            "pageSize":10000,
            "vaccineTypeId":"",
        },
        jsonType : "json",
        success : function(data) {
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.results;
            if(results!=null&&results.length>0){
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    vaccineArr.push(l.vaccineTypeName);
                    vaccineIdArr.push(l.vaccineTypeId);
                }
            }
            var html1="";
            if(vaccineArr.length>0){
                for(var j=0;j<vaccineArr.length;j++){
                    html1+="<option value='"+vaccineIdArr[j]+"'>"+vaccineArr[j]+"</option>";
                }
            }
            $("#vaccName").append(html1);
            $("#vaccineSec").append(html1);
            $('#vaccineSec').selectpicker('refresh');
            $('#vaccineSec').selectpicker('render');
        }
    })
    getbaseList();
}
/*查询*/
function getbaseList(init){
    JY.Model.loading();
    $("#vaccineSec").html("");
	$("#ticktTable tbody").html("");
    $("#baseTable2 tbody").html("");
    $("#baseTable2 tbody").append("<tr><td colspan='6' class='center'>暂无相关数据</td></tr>");
    var pageNum=$("#baseForm1 .pageNum").val();
    var pageSize=$("#baseForm1 .pageSize").val();
    var vaccName=$("#vaccName option:selected").val();
    if(vaccName=="全部"){
        vaccName=""
    }
	$.ajax({
		url : jypath + "/vaccine/queryVaccineType",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "vaccineTypeId":vaccName,
		},
		jsonType : "json",
		success : function(data) {
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    var jsonL=JSON.stringify(l);
                    html+= "<tr class='abc'>" ;
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+jsonL+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.vaccineTypeName+"</td>";
                    html+="<td class='center'>"+l.vaccinePart+"</td>";
                    html+="<td class='center'>"+l.totalAgent+"</td>";
                    html+="<td class='center'>"+l.vaccineResult+"</td>";
                    html+="<td class='center'>"+l.vaccineTaboo+"</td>";
                    html+="<td class='center'><a onclick='getbaseList2("+l.vaccineTypeId+")'>相关疫苗</a>，<a onclick='getAgent("+l.vaccineTypeId+")'>查看剂次</a></td>";
                    html+="</tr>"
                }
              /* vaccineTypeId*/
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm1","pageing",pageSize,pageNum,totalRecord,"getbaseList");
            }else{
                html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm1 ul").empty();//清空分页
            }
            JY.Model.loadingClose();
        }
	})
}
//相关疫苗查询
function getbaseList2(id){
    $("#baseTable2 tbody").html("");
    if(id==""||id==null||id==undefined){
        html+="<tr><td colspan='6' class='center'>暂无相关数据</td></tr>";
        $("#baseTable2 tbody").append(html);
    }else{
        var html="";
        $("#baseTable2 tbody").empty();
        /*JY.Model.loading();*/
        $.ajax({
            url : jypath + "/vaccine/queryVaccineRelevanceDetailPc",
            type : "post",
            data:{
                "vaccineTypeId":id,
            },
            jsonType : "json",
            success : function(data) {
               /* JY.Model.loadingClose();*/
                var html='';
                if (data.obj!=null&&data.obj.length!=0) {
                    for(var i=0;i<data.obj.length;i++){
                        var l=data.obj[i];
                        html+= "<tr>" ;
                        html+="<td   style=\"width: 5%\" class='center'>"+(i+1)+"</td>";
                        html+="<td class='center'>"+l.vaccineTypeName+"</td>";
                        html+="<td class='center'>"+l.vaccinePart+"</td>";
                        html+="<td class='center'>"+l.totalAgent+"</td>";
                        html+="<td class='center'>"+l.vaccineResult+"</td>";
                        html+="<td class='center'>"+l.vaccineTaboo+"</td>";
                        html+="</tr>"
                    }
                    $("#baseTable2 tbody").append(html)
                }else{
                    html+="<tr><td colspan='6' class='center'>暂无相关数据</td></tr>";
                    $("#baseTable2 tbody").append(html);
                }

            }
        })
    }
}
//新增疫苗
function add(){
    clearForm();
    JY.Model.edit("auForm_addVaccine","新增疫苗",function(){
        var that = $(this);
        var  vaccineTypeArr= $('#vaccineSec').val();
        if(vaccineTypeArr==null){
            vaccineTypeArr=[];
        }
        if(JY.Validate.form("addVaccineForm")){
            $(".masks").removeClass("hide");
            $.ajax({
                url : jypath + "/vaccine/insertVaccineType",
                type : "post",
                data:{
                    "vaccineTypeName":$("#addVaccineForm input[name='vaccine_name']").val().trim(),
                    "vaccinePart":$("#addVaccineForm textarea[name='vaccine_part']").val(),
                    "vaccineResult":$("#addVaccineForm textarea[name='vaccine_result']").val(),
                    "vaccineTaboo":$("#addVaccineForm textarea[name='vaccine_taboo']").val(),
                    "announcements":$("#addVaccineForm textarea[name='announcements']").val(),
                    "untowardEffect":$("#addVaccineForm textarea[name='untoward_effect']").val(),
                    "totalAgent":$("#addVaccineForm input[name='totalAgent']").val().trim(),
                    "parameter":vaccineTypeArr,
                },
                jsonType : "json",
                success : function(data) {
                    if(data.res==1){
                        that.dialog("close");
                        $(".masks").addClass("hide");
                        JY.Model.info("操作成功!");
                        getbaseList();
                        getbaseList2();
                    }
                }
            })
        }
    });
}
//配置剂次
function addAgent(id){
    clearAgent();
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var obj=JSON.parse(chks);
        var vaccineName=$('#ticktTable input[name="ids"]:checked').attr("names");
        $("#names").html(vaccineName);
        //查询下一针下拉框内容
        $.ajax({
            url : jypath + "/vaccine/queryVaccine",
            type : "post",
            data:{
                "vaccineTypeId":obj.vaccineTypeId,
            },
            jsonType : "json",
            success : function(data) {
                if(data.obj.results!=null&&data.obj.results.length!=0){
                    var html="";
                    for(var i=0;i<data.obj.results.length;i++){
                        var l=data.obj.results[i];
                        html+="<option value="+l.vaccineId+">"+l.vaccineTypeName+"，"+l.age+"，计划次数:"+l.inoculationOrder+"</option>"
                    }
                    $("#parent_id").append(html);
                }
            }
        })
        JY.Model.edit("auForm_addAgent","配置剂次",function(){
            var that=$(this);
            if($("#ageSec").find("option:selected").val()==""){
                JY.Model.info("请完善相关信息!");
            }else if($("#generateForm input[name='inoculation_order']").val()==""){
                JY.Model.info("请完善相关信息!");
            }else if($("#generateForm input[name='agent']").val()==""){
                JY.Model.info("请完善相关信息!");
            }else if($("#generateForm input[name='interval_time']").val()==""){
                JY.Model.info("请完善相关信息!");
            }else{
                $.ajax({
                    url : jypath + "/vaccine/insertVaccine",
                    type : "post",
                    data:{
                        "vaccineTypeId":obj.vaccineTypeId,
                        "age":$("#ageSec").find("option:selected").val(),
                        "inoculationOrder":$("#generateForm input[name='inoculation_order']").val(),
                        "agent":$("#generateForm input[name='agent']").val(),
                        "intervalTime":$("#generateForm input[name='interval_time']").val(),
                        "isCharge":$("#is_charge").find("option:selected").val(),
                        "parentId":$("#parent_id").find("option:selected").val(),
                    },
                    jsonType : "json",
                    success : function(data) {
                        that.dialog("close");
                        if(data.res==1){
                            JY.Model.info("操作成功!");
                        }else{
                            JY.Model.info(data.resMsg);
                        }
                    }
                })
            }
        });
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
//根据年龄查询已配置次数
function ageGetMess(){
    var ages=$("#ageSec option:selected").val();
    $("#alreadyVacc").html("");
    $.ajax({
        url : jypath + "/vaccine/queryVaccine",
        type : "post",
        data:{
            "age":ages,
        },
        jsonType : "json",
        success : function(data) {
            console.log(data)
            if(data.obj.results!=null&&data.obj.results.length!=0){
                //有相关疫苗
                $(".tips").removeClass("hide");
                var html="";
                for(var i=0;i<data.obj.results.length;i++){
                    var l=data.obj.results[i];
                    html+="<span>"+(i+1)+"、"+l.vaccineTypeName+"</span>"
                }
                $("#alreadyVacc").append(html);
            }else{
                //无相关疫苗
                $(".tips").addClass("hide")
            }
        }
    })

}
//查看剂次
function getAgent(id){
    $("#checkAgentTable tbody").html("");
    $.ajax({
        url : jypath + "/vaccine/queryVaccine",
        type : "post",
        data:{
            "vaccineTypeId":id,
        },
        jsonType : "json",
        success : function(data) {
            if(data.obj.results!=null&&data.obj.results.length>0){
                var html="";
                for(var i=0;i<data.obj.results.length;i++){
                  var k=data.obj.results[i];
                    html+="<tr>"
                    html+="<td class=\"center\">"+k.age+"</td>"
                    html+="<td class=\"center\">"+k.inoculationOrder+"</td>"
                    html+="<td class=\"center\">"+k.agent+"</td>"
                    html+="<td class=\"center\">"+k.intervalTime+"</td>"
                    if(k.isCharge==0){
                        html+="<td class=\"center\">否</td>"
                    }else if(k.isCharge==1){
                        html+="<td class=\"center\">自费</td>"
                    }
                    html+="<td class=\"center\">"+k.vaccineTypeName+"，"+k.age+"，计划次数："+k.inoculationOrder+"</td>"
                    html+="</tr>"
                }
                $("#checkAgentTable tbody").append(html);
                JY.Model.check("auForm_checkAgent","查看剂次",function(){

                })
            }else{
                JY.Model.info("暂未查到相关剂次，请先配置剂次!");
            }
        }
    })
}
//编辑
function edit(){
    var chks =[];
    clearForm();
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var obj=JSON.parse(chks);
        $("#addVaccineForm input[name='vaccine_name']").val(obj.vaccineTypeName);
        $("#addVaccineForm textarea[name='vaccine_part']").val(obj.vaccinePart);
        $("#addVaccineForm textarea[name='vaccine_result']").val(obj.vaccineResult);
        $("#addVaccineForm textarea[name='vaccine_taboo']").val(obj.vaccineTaboo);
        $("#addVaccineForm textarea[name='announcements']").val(obj.announcements);
        $("#addVaccineForm textarea[name='untoward_effect']").val(obj.untowardEffect);
        $("#addVaccineForm input[name='totalAgent']").val(obj.totalAgent);
        $.ajax({
            url : jypath + "/vaccine/queryVaccineRelevanceDetailPc",
            type : "post",
            data:{
                "vaccineTypeId":obj.vaccineTypeId,
            },
            jsonType : "json",
            success : function(data) {
                var arrId=[];
                $('#addVaccineForm .selectpicker').selectpicker('val','').trigger("change");
                if(data.obj.length>0&&data!=null){
                    for(var k=0;k<data.obj.length;k++){
                        arrId.push(data.obj[k].vaccineTypeId)
                    }
                }else{
                    arrId=[];
                }
                $('.selectpicker').selectpicker('val', arrId).trigger("change");
                JY.Model.edit("auForm_addVaccine","编辑",function(){
                    var that = $(this);
                    var  vaccineTypeArr= $('#vaccineSec').val();
                    if(vaccineTypeArr==null){
                        vaccineTypeArr=[];
                    }
                    if(JY.Validate.form("addVaccineForm")){
                        $.ajax({
                            url : jypath + "/vaccine/updateVaccineType",
                            type : "post",
                            data:{
                                "vaccineTypeId":obj.vaccineTypeId,
                                "vaccineTypeName":$("#addVaccineForm input[name='vaccine_name']").val().trim(),
                                "vaccinePart":$("#addVaccineForm textarea[name='vaccine_part']").val(),
                                "vaccineResult":$("#addVaccineForm textarea[name='vaccine_result']").val(),
                                "vaccineTaboo":$("#addVaccineForm textarea[name='vaccine_taboo']").val(),
                                "announcements":$("#addVaccineForm textarea[name='announcements']").val(),
                                "untowardEffect":$("#addVaccineForm textarea[name='untoward_effect']").val(),
                                "totalAgent":$("#addVaccineForm input[name='totalAgent']").val().trim(),
                                "parameter":vaccineTypeArr,
                            },
                            jsonType : "json",
                            success : function(data) {
                                if(data.res==1){
                                    that.dialog("close");
                                    JY.Model.info("操作成功!");
                                    getbaseList();
                                    getbaseList2();
                                }
                            }
                        })
                    }
                })
            }
        })
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
function clearForm(){
    $("#addVaccineForm input[name='vaccine_name']").val("");
    $("#addVaccineForm .textVaccine").val("");
}
function clearAgent(){
    $("#ageSec").find("option[value = '']").prop("selected","selected");
    $("#generateForm input").val("");
    $("#generateForm #is_charge").find("option[value = '0']").prop("selected","selected");
    $("#generateForm #parent_id").find("option[value = '']").prop("selected","selected");
    $('#addVaccineForm .selectpicker').selectpicker('val','').trigger("change");
    $(".tips").addClass("hide");
}
